package edu.mis.modules.orderList;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Record;
import edu.mis.model.OrderList;
import edu.mis.model.Seat;
import edu.mis.model.Transaction;


public class OrderListService {
	
	/**
	 * 添加商品
	 * @param orderList
	 * @return
	 */
	public boolean addOrderList(OrderList orderList){
			return orderList.save();

	}
    /**
     * 修改商品信息
     * @param orderList
     * @return
     */
    public boolean updateOrderList(OrderList orderList){
            return orderList.update();
    }
	

	

	
	/**
	 * 根据ID得到商品
	 * @param id
	 * @return
	 */
	public OrderList findOrderListById(int id) {
		return OrderList.dao.findById(id);
	}
	
	/**
	 * 查询所有商品
	 * @return
	 */
	public List<OrderList> findAll(){
		return OrderList.dao.findAll();
	}
	/**
	 * 查询所有商品及其权限
	 * @return
	 */
	public List<Record> findAllOrderListWithSeat(){
		return OrderList.dao.findAllRecord();
	}
	public List<Record> findAllUnpaidRecord(){
		return OrderList.dao.findAllUnpaidRecord();
	}
	/**
	 * 查询订单详细
	 * @return
	 */
	public List<Record> findAllOrderDetailsById(int id){
		return OrderList.dao.findAllOrderDetailsWithCommodity(id);
	}

	/**
	 * 商品分类
	 * @param id
	 * @param seatId
	 * @return
	 */
	public boolean classifyOrderList(int id, int seatId) {
		OrderList orderList = findOrderListById(id);
		orderList.setSeatId(seatId);
		return orderList.update();
	}
	/**
	 * 删除商品
	 * @param id
	 * @return
	 */
	public boolean deleteOrderListById(int id) {
		return OrderList.dao.deleteById(id);
	}


	public OrderList findOrderListByDate(Date orderTime) {
		return OrderList.dao.findOrderListByDate(orderTime);
	}

	/**
	 * 订单结账
	 * @param id
	 * @param orderType
	 * @return
	 */
	public boolean checkOutOrderList(int id,int seatId, int orderType) {
		OrderList orderList = findOrderListById(id);
		orderList.setOrderTypeId(orderType);
		Seat seat = orderList.findSeatBySeatId(seatId).setSeatTypeId(1);
		orderList.setSeat(seat);
		seat.update();
		Transaction transaction = new Transaction();
		transaction.setOrderId(orderList.getId());
		transaction.setSeatId(orderList.getSeatId());
		transaction.setOrderTime(orderList.getOrderTime());
		transaction.setTransactionTime(new Date());
		transaction.setTotalPrice(orderList.getTotalPrice());
		transaction.setOrderTypeId(orderList.getOrderTypeId());
		transaction.save();
		return orderList.update();
	}
}
